package com.zhu.ttwords.view.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zhu.ttwords.R;
import com.zhu.ttwords.TTWORDS;
import com.zhu.ttwords.presenter.RegisterPresenter;

public class RegisterActivity extends ActionBarActivity implements IRegisterActivity {
    public static final String USERNAME = "USERNAME";
    public static final String PASSWORD = "PASSWORD";

    RegisterPresenter presenter;
    EditText username;
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username = (EditText) this.findViewById(R.id.activity_regist_username);
        password = (EditText) this.findViewById(R.id.activity_regist_password);
        presenter = new RegisterPresenter(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_register, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void submit(View view) {
        presenter.registAction(username.getText().toString(), password.getText().toString());
    }

    @Override
    public void showRegistResult(int result) {
        //todo
        switch (result) {
            case TTWORDS.TRANS_SUCCESS:
                Intent intent = getIntent();
                intent.putExtra(USERNAME, username.getText().toString());
                intent.putExtra(PASSWORD, password.getText().toString());
                setResult(RESULT_OK, intent);
                this.finish();
                break;
            case TTWORDS.TRANS_SERVER_WRONG:
                Toast.makeText(this, R.string.warning_regist_used, Toast.LENGTH_SHORT).show();
                break;
            case TTWORDS.TRANS_REGISTER_USED:
                Toast.makeText(this, R.string.warning_regist_used, Toast.LENGTH_SHORT).show();
                break;

        }

    }
}
